export function testUtil(vm) {

}

/**
 *
 * @param {对象} obj
 * @param {key名} name
 * @returns obj对象下的name键的value值
 */
export function getValue(obj, name) {
  if (!obj) {
    return obj;
  }
  let nameList = name.split(".");
  let temp = obj;
  for (let i = 0; i < nameList.length; i++) {
    const key = nameList[i];
    if (key) {
      temp = temp[key];
    } else {
      return undefined;
    }
  }
  return temp;
}

export function setValue(obj, data, value) {
  if (!obj) {
    return;
  }
  let attList = data.split(".");
  let temp = obj;
  let len = attList.length - 1;
  for (let i = 0; i < len; i++) {
    const name = attList[i];
    if (name) {
      temp = temp[name];
    } else {
      return;
    }
  }
  if (temp[attList[len]] != null) {
    temp[attList[len]] = value;
  }
}

//合并两个对象
export function mergeAttr(obj1, obj2) {
  if (obj1 == null) {
    return clone(obj2);
  }
  if (obj2 == null) {
    return clone(obj1);
  }
  let result = {};
  let obj1names = Object.getOwnPropertyNames(obj1);
  let obj2names = Object.getOwnPropertyNames(obj2);
  for (let i = 0; i < obj1names.length; i++) {
      const key = obj1names[i];
      result[key] = obj1[key];
  }
  for (let i = 0; i < obj2names.length; i++) {
      const key = obj2names[i];
      result[key] = obj2[key];
  }
  return result
}

//克隆算法
export function clone(obj) {
  if (obj instanceof Array) {
    return cloneArray(obj);
  } else if (obj instanceof Object) {
    return cloneObject(obj);
  } else {
    return obj;
  }

  function cloneObject(obj) {
    let result = {};
    let names = Object.getOwnPropertyNames(obj);
    for (let i = 0; i < names.length; i++) {
      result[names[i]] = clone(obj[names[i]]);
    }
    return result;
  }

  function cloneArray(obj) {
    let result = new Array(obj.length);
    for (let i = 0; i < obj.length; i++) {
      result[i] = clone(obj[i]);
    }
    return result;
  }
}


export function getEnvAttr(vm,vnode){
  let result = mergeAttr(vm._data,vnode.env);
  return result;
}